package org.mch.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.mch.comm.itf.IMLogger;
import org.mch.comm.loging.MComLogger;
import org.mch.frame.itf.IMFrame;
import org.mch.main.panel.MainPanel;
import org.mch.start.MCHStart;
import org.mch.swingm.core.MButton;

import com.mch.main.common.MDebugDLG;
import com.mch.main.common.MEnv;
import com.mch.ui.cons.MStyle;

public class MCHFrame extends JFrame implements IMFrame {
	private static final long serialVersionUID = 6051046861721612525L;
	private final static IMLogger log = new MComLogger(MCHFrame.class);

	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu menu_file;
	private JMenu menu_edit;
	private JMenu menu_help;
	private JMenuItem mntmAbout;

	private MCHFrameEvent event;
	private JPanel panel;
	private JButton btnLog;
	private MainPanel panelMain;
	private JPanel panelBar;
	private JLabel labelCenter;

	public static void main(String[] args) {
		MStyle.initAllStyle();

		MCHStart.loadLogConfig();
		log.debug("Start......");

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MEnv.MAINFRAME = new MCHFrame();
					// MCHFrame.frame.showMax();

					// Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包
					// Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸
					// int screenWidth = screenSize.width / 2; // 获取屏幕的宽
					// int screenHeight = screenSize.height / 2; // 获取屏幕的高
					// int height = MCHFrame.frame.getHeight();
					// int width = MCHFrame.frame.getWidth();
					// // 移动TestFrame至居中位置
					// MCHFrame.frame.setLocation(screenWidth - width / 2,
					// screenHeight - height / 2 - 50);
					MEnv.MAINFRAME.setVisible(true);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public MCHFrame() {
		super();
		initGUI();
	}

	private int defWidth = 1152;
	private int defHeight = 720;

	private void initGUI() {
		setSize(new Dimension(700, 500));
		setIconImage(Toolkit.getDefaultToolkit().getImage(MCHFrame.class.getResource("/resource/images/title.jpg")));
		setTitle(MEnv.get("mchtitle"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		contentPane.add(getPanelMain(), BorderLayout.CENTER);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getPanelBar(), BorderLayout.SOUTH);
		setContentPane(contentPane);

		buildFrameXY(defWidth, defHeight);// 构建主界面是否居中或最大化，应放在构建的最后一行；
	}

	/**
	 * 根据窗口的宽、高计算出相对屏幕的X、Y坐标
	 * 
	 * @param width
	 * @param height
	 * @return：int[0]=X，int[1]=Y；
	 */
	private void buildFrameXY(int width, int height) {
		Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包
		Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸
		if (screenSize.width > 1366) {
			// 居中
			int screenWidth = screenSize.width / 2; // 获取屏幕的宽
			int screenHeight = screenSize.height / 2; // 获取屏幕的高

			// 移动TestFrame至居中位置
			int[] xy = new int[] { screenWidth - width / 2, screenHeight - height / 2 - 18 };

			setBounds(xy[0], xy[1], defWidth, defHeight);

		} else {
			// 否则，最大化
			setExtendedState(JFrame.MAXIMIZED_BOTH);
		}
	}

	public MCHFrameEvent getEvent() {
		if (event == null) {
			event = new MCHFrameEvent(this);
		}
		return event;
	}

	public JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMenu_file());
			menuBar.add(getMenu_edit());
			menuBar.add(getMenu_help());
		}
		return menuBar;
	}

	public JMenu getMenu_file() {
		if (menu_file == null) {
			menu_file = new JMenu("File");
			menu_file.setFont(new Font("宋体", Font.PLAIN, 13));
			menu_file.add(getMntmExit());
		}
		return menu_file;
	}

	public JMenu getMenu_edit() {
		if (menu_edit == null) {
			menu_edit = new JMenu("Edit");
			menu_edit.setFont(new Font("宋体", Font.PLAIN, 13));
			menu_edit.add(getMenu_find());
		}
		return menu_edit;
	}

	public JMenu getMenu_help() {
		if (menu_help == null) {
			menu_help = new JMenu("Help");
			menu_help.setFont(new Font("宋体", Font.PLAIN, 13));
			menu_help.setToolTipText("帮助");
			menu_help.add(getMntmAbout());
		}
		return menu_help;
	}

	public JMenuItem getMntmAbout() {
		if (mntmAbout == null) {
			mntmAbout = new JMenuItem("About");
			mntmAbout.setFont(new Font("宋体", Font.PLAIN, 13));
			mntmAbout.setToolTipText("关于MCH");
			mntmAbout.addActionListener(getEvent());
		}
		return mntmAbout;
	}

	public JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			FlowLayout flowLayout = (FlowLayout) panel.getLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			panel.add(getBtnLog());
		}
		return panel;
	}

	private JMenuItem mntmExit;
	private JMenuItem menu_find;
	private JLabel labelLeft;
	private JLabel labelRight;

	public JButton getBtnLog() {
		if (btnLog == null) {
			btnLog = new MButton("调试日志");
			btnLog.setFont(new Font("宋体", Font.PLAIN, 13));
			btnLog.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					MDebugDLG.getInstance().setVisible(true);
				}
			});
		}
		return btnLog;
	}

	public MainPanel getPanelMain() {
		if (panelMain == null) {
			panelMain = new MainPanel();
		}
		return panelMain;
	}

	public JPanel getPanelBar() {
		if (panelBar == null) {
			panelBar = new JPanel();
			panelBar.setLayout(new BorderLayout(0, 0));
			panelBar.add(getLabelLeft(), BorderLayout.WEST);
			panelBar.add(getLabelCenter());
			panelBar.add(getLabelRight(), BorderLayout.EAST);
		}
		return panelBar;
	}

	public JLabel getLabelCenter() {
		if (labelCenter == null) {
			labelCenter = new JLabel(" ");
			labelCenter.setForeground(Color.RED);
			labelCenter.setFont(new Font("宋体", Font.PLAIN, 13));
		}
		return labelCenter;
	}

	public void showMax() {
		// 最大化显示Frame，必需先执行此方法，否则无效；
		pack();
		setExtendedState(JFrame.MAXIMIZED_BOTH);// 窗口初始最大化

		setVisible(true);
	}

	public JMenuItem getMntmExit() {
		if (mntmExit == null) {
			mntmExit = new JMenuItem("Exit");
			mntmExit.setFont(new Font("宋体", Font.PLAIN, 13));
			mntmExit.setToolTipText("退出");
			mntmExit.addActionListener(getEvent());
		}
		return mntmExit;
	}

	public JMenuItem getMenu_find() {
		if (menu_find == null) {
			menu_find = new JMenuItem("Find");
			menu_find.setFont(new Font("宋体", Font.PLAIN, 13));
		}
		return menu_find;
	}

	public JLabel getLabelLeft() {
		if (labelLeft == null) {
			labelLeft = new JLabel("New label");
			labelLeft.setFont(new Font("宋体", Font.PLAIN, 13));
			labelLeft.setPreferredSize(new Dimension(300, 18));
		}
		return labelLeft;
	}

	public JLabel getLabelRight() {
		if (labelRight == null) {
			labelRight = new JLabel("New label");
			labelRight.setFont(new Font("宋体", Font.PLAIN, 13));
			labelRight.setPreferredSize(new Dimension(200, 18));
		}
		return labelRight;
	}

	public void setMsg(String msg) {
		getLabelCenter().setText("");
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		getLabelCenter().setText(msg);
	}
}
